// Copyright (c) Appeon. All rights reserved.
// Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. 
// You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0.

using System.Data;
using JetBrains.Annotations;

namespace SnapObjects.Data.Odbc
{
    public class OdbcBitByteArrayTypeMapping : OdbcByteArrayTypeMapping
    {

        /// <summary>
        ///     Initializes a new instance of the <see cref="OdbcByteArrayTypeMapping" /> class.
        /// </summary>
        /// <param name="dbTypeName"> The name of the database type. </param>
        /// <param name="dbType"> The <see cref="System.Data.DbType" /> to be used. </param>
        /// <param name="size"> The size of data the property is configured to store, or null if no size is configured. </param>
        public OdbcBitByteArrayTypeMapping(
            [NotNull] string dbTypeName,
            [CanBeNull] DbType? dbType = System.Data.DbType.Binary,
            int? size = null)
            : base(dbTypeName, dbType, size)
        {
            _maxSpecificSize = this.CalculateSize(size);
        }

        protected override int CalculateSize(int? size)
        {
            return size.HasValue && size < 4095 ? size.Value : 4095;
        }

        public override TypeMapping Clone(string dbTypeName, int? size)
        {
            return new OdbcBitByteArrayTypeMapping(
                           dbTypeName,
                           this.DbType,
                           size);
        }
    }
}
